home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_127 / bounce / bounce.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  223 lines

  1.     /********************************************************
  2.     *                            *
  3.     *            Bounce                 *
  4.     *            ======                *
  5.     *                            *
  6.     * Authors: Steve Hansel and Tom Hansel            *
  7.     *                            *
  8.     * This program creates little dots that bounce around    *
  9.     * and multiply.  These dots (balls) react to collisions *
  10.     * in the following manner:                *
  11.     *                            *
  12.     * 1) If a ball hits a wall of color 2, it bounces off     *
  13.     *    in an elastic collision.                *
  14.     *                            *
  15.     * 2) If a ball hits a wall of color 1 or another ball    *
  16.     *    it bounces off with a random speed and direction.    *
  17.     *                            *
  18.     * 3) If a ball hits a red pixel, it divides.        *
  19.     *                            *
  20.     * This program will run until someone hits the close    *
  21.     * gadget.                        *
  22.     *                            *
  23.     ********************************************************/
  24.  
  25. #include "exec/types.h"
  26. #include "exec/tasks.h"
  27. #include "exec/libraries.h"
  28. #include "exec/devices.h"
  29. #include "devices/keymap.h"
  30. #include "graphics/copper.h"
  31. #include "graphics/display.h"
  32. #include "graphics/gfxbase.h"
  33. #include "graphics/text.h"
  34. #include "graphics/view.h"
  35. #include "graphics/gels.h"
  36. #include "graphics/regions.h"
  37. #include "hardware/blit.h"
  38. #include "intuition/intuition.h"
  39. #include "intuition/intuitionbase.h"
  40.  
  41. struct Ball
  42. {  short x,y,vx,vy;
  43.    struct Ball *next;
  44. };
  45.  
  46. struct GfxBase *GfxBase;   /* Export library base pointers */
  47. struct IntuitionBase *IntuitionBase;
  48. struct IntuiMessage *message;
  49. struct Screen *screen;
  50.  
  51. struct Window *window;
  52. struct RastPort *rport;
  53.  
  54. struct NewWindow nwindow =
  55. {  0,10,639,175,
  56.    1,2,CLOSEWINDOW, SMART_REFRESH | WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE,
  57.    NULL,NULL,(char *)"Bounce By SJH & TJH",
  58.    NULL,NULL,
  59.    20,20,650,100,WBENCHSCREEN
  60. };
  61.  
  62. struct Ball firstball = {120,120,2,3,NULL},*curball,*tempball;
  63.  
  64. main ()
  65. {
  66. short x,y;
  67. char flag = TRUE;
  68. short random ();
  69.  
  70.    if ((IntuitionBase = (struct  IntuitionBase *)OpenLibrary("intuition.library",0))==0)
  71.    {  printf("Problems opening intuition\n");
  72.       exit ();
  73.    }
  74.  
  75.    if ((GfxBase = (struct  GfxBase *) OpenLibrary("graphics.library",0))==0)
  76.    {  printf ("Problems opening graphics library\n");
  77.       CloseLibrary (IntuitionBase);
  78.       exit ();
  79.    }
  80.  
  81.    window = (struct Window *)OpenWindow(&nwindow);
  82.    if (window == 0)
  83.    {  printf ("Window Failed\n");
  84.       CloseScreen (screen);
  85.       CloseLibrary (GfxBase);
  86.       CloseLibrary (IntuitionBase);
  87.       exit ();
  88.    };
  89.  
  90.    rport = window -> RPort;
  91.  
  92.    SetAPen (rport,3L);
  93.    Move (rport,320L,75L);
  94.    Draw (rport,320L,115L);
  95.  
  96.  
  97.    SetAPen (rport,2L);
  98.    Move (rport,60L,160L);
  99.    Draw (rport,60L,30L);
  100.    Draw (rport,350L,30L);
  101.  
  102.    for (x=80;x<300;x+=10)
  103.    { Move (rport,(long)x,150L);
  104.      Draw (rport,(long)x,120L);
  105.    }
  106.  
  107.    SetAPen (rport,1L);
  108.    for (x=400;x<500;x+=5)
  109.       for (y=40;y<100;y+=7)
  110.          WritePixel (rport,(long)x,(long)y);
  111.  
  112.    Move (rport,40L,170L);
  113.    Draw (rport,250L,170L);
  114.  
  115.    while (flag)
  116.    {  curball = &firstball;
  117.       while (curball != NULL)
  118.       {  SetAPen (rport,0L);
  119.          WritePixel (rport,(long)(curball -> x >> 2), (long)(curball -> y >> 2));
  120.  
  121.          x = curball -> x + curball -> vx;
  122.          y = curball -> y + curball -> vy;
  123.  
  124.          switch (ReadPixel (rport,(long)(x >> 2), (long)(y >> 2)))
  125.          {  case 3:
  126.             NewBall (curball);
  127.             break;
  128.  
  129.             case 1:
  130.             do
  131.             {  curball -> vx = random () - 4;
  132.                curball -> vy = random () - 4;
  133.             }
  134.             while (!(curball -> vx && curball -> vy));
  135.             break;
  136.          }
  137.          if (ReadPixel (rport,(long)(x >> 2),(long)(curball -> y >> 2)))
  138.          {  curball -> vx = - curball -> vx;
  139.             x = curball -> x;
  140.          }
  141.  
  142.          if (ReadPixel (rport,(long)(curball -> x >> 2),(long)(y >> 2)))
  143.          {  curball -> vy = - curball -> vy;
  144.             y = curball -> y;
  145.          }
  146.  
  147.          if (ReadPixel (rport,(long)(x >> 2),(long)(y >> 2)))
  148.          {  curball -> vy = - curball -> vy;
  149.             curball -> vx = - curball -> vx;
  150.             x = curball -> x;
  151.             y = curball -> y;
  152.          }
  153.  
  154.          SetAPen (rport,1L);
  155.          WritePixel (rport,(long)(x >> 2),(long)(y >> 2));
  156.  
  157.          curball -> x = x;
  158.          curball -> y = y;
  159.          curball = curball -> next;
  160.       }
  161.  
  162.       if (message = (struct IntuiMessage *) GetMsg (window -> UserPort))
  163.       {  flag = (message -> Class != CLOSEWINDOW);
  164.          ReplyMsg (message);
  165.       }
  166.    }
  167.  
  168.    curball = firstball.next;
  169.    while (curball != NULL)
  170.    {  tempball = curball -> next;
  171.       FreeMem (curball,(long)sizeof (struct Ball));
  172.       curball = tempball;
  173.    }
  174.  
  175.    CloseWindow (window);
  176.    CloseLibrary (GfxBase);
  177.    CloseLibrary (IntuitionBase);
  178. }
  179.  
  180. NewBall (splitball)
  181.  
  182. struct Ball *splitball;
  183. {
  184. struct Ball *newball;
  185. short i,j,random();
  186. static short count = 1;
  187.  
  188.    if (newball = (struct Ball *) AllocMem ((long)sizeof (struct Ball),0L))
  189.    {  newball -> next = firstball.next;
  190.       firstball.next = newball;
  191.  
  192.       i = -2; j = -2;
  193.  
  194.       while (ReadPixel (rport,(long)(i + (splitball -> x >> 2)),
  195.          (long)(j + (splitball -> y >> 2))))
  196.       {  ++i;
  197.          if (i > 1)
  198.          {  i = -2;
  199.             ++j;
  200.          }
  201.       }
  202.       newball -> x = i * 4 + splitball -> x;
  203.       newball -> y = j * 4 + splitball -> y;
  204.  
  205.       do
  206.       {  newball -> vx = random () - 4;
  207.          newball -> vy = random () - 4;
  208.       }
  209.       while (!(newball -> vx && newball -> vy));
  210.    }
  211. }
  212.  
  213. short random ()
  214. {
  215.    static short number = 143;
  216.    number =   (((number & 1) ^ (number >> 1 & 1)) ^ ((number >> 3 & 1) ^ (number >> 12 & 1))) << 14 | (number >> 1);
  217.    return (short) (number & 7);
  218. }
  219.  
  220.  
  221.  
  222.  
  223.